package com.adayo.blesdkdemo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.adayo.ble.BluetoothClientBLEV2Adapter;
import com.adayo.ble.BluetoothLeInitialization;
import com.adayo.ble.bean.BLEDevice;
import com.adayo.ble.callback.BluetoothClient;
import com.adayo.ble.callback.OnSearchListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Android蓝牙4.0连接通讯测试程序
 * 2017.08.19
 * edit by dzt
 */
public class MainActivity extends AppCompatActivity implements
		AdapterView.OnItemClickListener,
		View.OnClickListener{

	private BluetoothClient mClient;
	private ListView listView;
	private List<BLEDevice> list = new ArrayList<>();
	private BtAdapter adapter;
	private BLEDevice device;

	private Handler handler = new Handler(new Handler.Callback() {
		@Override
		public boolean handleMessage(Message message) {
			if (message.what == 1) {
				String str = (String) message.obj;
				//mTextView.setText(str);
			}else if(message.what == 2){
				adapter.notifyDataSetChanged();
			}
			return false;
		}
	});

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mClient = new BluetoothClientBLEV2Adapter(BluetoothLeInitialization.getInstance());
		mClient.openBluetooth();

		findViewById(R.id.btn_scan).setOnClickListener(this);
		findViewById(R.id.btn_connect).setOnClickListener(this);
		findViewById(R.id.btn_write).setOnClickListener(this);

		listView = (ListView) findViewById(R.id.list);
		listView.setOnItemClickListener(this);
		adapter = new BtAdapter(this, list);
		listView.setAdapter(adapter);
	}

	private void search() {
		mClient.search(3000, false, new OnSearchListener() {
			private Message msg;

			@Override
			public void onStart() {
				msg = handler.obtainMessage();
				msg.what = 1;
				msg.obj = "start\n";
				handler.sendMessage(msg);
				list.clear();
			}

			@Override
			public void onFindDevice(BLEDevice device) {
				msg = handler.obtainMessage();
				msg.what = 2;
				//msg.obj = mTextView.getText() + "\n\n" + device;
				//msg.obj = device;
				handler.sendMessage(msg);
				list.add(device);
			}

			@Override
			public void onComplete() {
				msg = handler.obtainMessage();
				msg.what = 1;
				//msg.obj = mTextView.getText() + "\n\n" + "complete";
				handler.sendMessage(msg);
			}

			@Override
			public void onError(String err) {
				msg = handler.obtainMessage();
				msg.what = 1;
				//msg.obj = mTextView.getText() + "\n\n" + "onError : " + err;
				handler.sendMessage(msg);
			}
		});
	}

	private void write() {
		if(device != null){
			mClient.write(device.getMac(), "00222222".getBytes());
		}
	}

	private void connect() {
		if(device != null){
			mClient.connect(device.getMac());
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(device != null){
			mClient.disconnect(device.getMac());
		}
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
		device = list.get(i);
		mClient.connect(device.getMac());
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()){
			case R.id.btn_scan:
				search();
				break;
			case R.id.btn_connect:
				connect();
				break;
			case R.id.btn_write:
				write();
				break;
			default:
				break;
		}
	}
}
